<template>
	<view class="cut_payment_box">
		<!-- 收货地址信息 -->
		<view class="address-section" @click="gotoAddress">
			<view class="order-content">
				<text class="yticon icon-shouhuodizhi"></text>
				<view class="cen" v-if="Object.keys(showPageAddressObj).length != 0">
					<view class="top">
						<text class="name">{{ showPageAddressObj.realname }}</text>
						<text class="mobile">{{ showPageAddressObj.telephone }}</text>
					</view>
					<text
						class="address">{{ showPageAddressObj.provinceStr || '' }}{{ showPageAddressObj.cityStr || '' }}{{ showPageAddressObj.areaStr || '' }}{{ showPageAddressObj.street || '' }}</text>
				</view>
				<view class="cen" v-else>请填写收货地址</view>
				<text class="yticon icon-you"></text>
			</view>
			<image src="static/line11.png" class="a-bg" />
		</view>
		<!-- 商品详情 -->
		<view class="pro_box">
			<image class="imgs" mode="aspectFit" :src="getFirstImg(proDetailObj.images)">
			</image>
			<view class="pro_right">
				<text class="protitle">{{proDetailObj.name}}</text>
				<view class="cut_price">{{proDetailObj.type==2?'额度奖品':proDetailObj.type==1?'黄金奖品':'实物奖品'}}</view>
				<text class="old_price">价值：￥{{proDetailObj.price}}</text>
				<text class="end_time">活动时间截止 {{proDetailObj.endDate}}</text>
			</view>
		</view>
		<view class="other_tips">
			<view class="kuadi">
				<text style="font-weight: 600;">快递费用</text>
				<text style="color: #888;">到付</text>
			</view>
			<view class="kuadi" v-if="proDetailObj.type!=2">
				<text style="font-weight: 600;">商品已踢至</text>
				<text style="color: #888;">￥{{(proDetailObj.price-proDetailObj.kanPrice).toFixed(2)}}</text>
			</view>
			<view class="kuadi" v-if="proDetailObj.type==2">
				<text style="font-weight: 600;">已免费获得额度</text>
				<text style="color: #888;">￥{{proDetailObj.price.toFixed(2)}}</text>
			</view>
		</view>
		<!-- 支付方式 -->
		<!-- 底部的按钮 -->
		<view class="submit_btn">
			<view class="total_price">合计：<text
					style="color: #8c6c49;">￥{{(proDetailObj.price-proDetailObj.kanPrice).toFixed(2)}}</text></view>
			<view class="btn" @click="submit">立即免费拿</view>
		</view>


		<wyb-popup ref="dizhiText" type="bottom" height="500" width="750" radius="6" :showCloseIcon="true">
			<view class="dizhiText">
				<view class="dizhi_tit">选择地址</view>
				<view style="height: 300rpx;">
					<scroll-view :scroll-top="scrollTop" scroll-y="true" style="height: 100%;">
						<view class="dizhi_item" v-for="(item,index) in addressList" :key="index"
							@click="exChangeDizhi(item)">
							<text class="yticon icon-shouhuodizhi"></text>
							<view class="dizhi">
								<view class="dizhi1"><text
										style="margin-right: 5rpx;font-weight: 600;">{{ item.realname }}</text>{{ item.telephone }}
								</view>
								<view class="dizhi2">
									{{ item.provinceStr || '' }}{{ item.cityStr || '' }}{{ item.areaStr || '' }}{{ item.street || '' }}
								</view>
							</view>
							<text class="yticon icon-iconfontshanchu1 shanchuBtn"
								@click.stop="deleteProvince(item.id)"></text>
						</view>
					</scroll-view>
				</view>

				<view class="newAddress" @click="toNewAddress">新建地址</view>
			</view>
		</wyb-popup>
	</view>
</template>

<script>
	import {
		getUserId
	} from '@/utils';
	export default {
		name: '',
		props: {},
		data() {
			return {
				showPageAddressObj: {}, //每次展示到页面的诗句
				addressList: [],
				scrollTop: 0,
				address: '',
				zfbHasPayment: true,
				wxHasPayment: true,
				prizeId: '', //产品id
				proDetailObj: {},
				proDetailImgList: [],
			};
		},
		components: {},

		onLoad(e) {
			console.log('砍单支付页面eeee---', e);
			this.prizeId = e.prizeId
			this.getCutProDetail()
			this.onList()
			this.getPaymentList()
		},
		onUnload() {},
		onShow() {
			this.onList()
		},
		methods: {
			//获取砍价商品详情信息
			getCutProDetail(prizeId, prizeUid) {
				this.$request(
					'apiActivityPrize/findActivityDetail', {
						prizeId: this.prizeId,
						uid: getUserId()
					},
					'post',
					false,
					true
				).then((res) => {
					if (res.businessSuccess || res.success) {
						this.proDetailObj = res.data
						if (res.data.detail) {
							this.proDetailImgList = this.imgRegSrc(res.data.detail);
							console.log('相片详情图片', this.proDetailImgList);
						}
						//计算商品失效过期倒计时
						// if (new Date().getTime() > new Date(this.proDetailObj.endDate).getTime() || new Date()
						// 	.getTime() > new Date(this.proDetailObj.overdueTime).getTime()) {
						// 	//过期
						// } else {
						// 	if (new Date(this.proDetailObj.endDate).getTime() >= new Date(this.proDetailObj
						// 			.overdueTime)
						// 		.getTime()) {
						// 		this.mistiming(new Date(this.proDetailObj.overdueTime).getTime())
						// 	} else {
						// 		this.mistiming(new Date(this.proDetailObj.endDate).getTime())
						// 	}
						// }
					}
				});
			},
			getFirstImg(imagesStr) {
				if (imagesStr) {
					return imagesStr.split(',')[0]
				}
			},
			submit() {
				if (Object.keys(this.showPageAddressObj).length == 0 || !this.showPageAddressObj.id) {
					uni.showToast({
						icon: 'none',
						title: '请填写收货地址'
					});
					return false;
				}
				uni.showToast({
					icon: 'none',
					title: '加载中'
				});
				this.$request(
					'apiActivityPrize/acticityPrizePay', {
						prizeOrderId: this.proDetailObj.prizeOrderId,
						uid: getUserId(),
						addressId: this.showPageAddressObj.id
					},
					'post',
					false,
					true
				).then((res) => {
					if (res.businessSuccess || res.success) {
						uni.hideLoading();
						this.$onTo(`pagesInfo/cutOrder/cutOrder`, false, true)
					} else {
						uni.showToast({
							title: res.msg || res.message,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			//删除地址
			deleteProvince(id) {
				this.$request(
					"member/mall/userAddress/deleteUserAddress", {
						id: id
					},
					"get",
					true
				).then((res) => {
					this.onList()
				})
			},
			//去新建地址
			toNewAddress() {
				this.$onTo(`pagesInfo/setAddress-rich/setAddress-rich`)
			},
			//切换地址
			exChangeDizhi(item) {
				this.showPageAddressObj = item
				this.$refs.dizhiText.close();
			},
			//获取地址列表
			onList() {
				this.$request("member/mall/userAddress/getUserAddress", {}, "get").then(
					(res) => {
						this.addressList = res.data || []
						if (this.addressList.length > 0) {
							this.$refs.dizhiText.close();
							let obj = {};
							this.addressList.forEach((item, index) => {
								if (item.isDefault) {
									obj = item;
									this.addressList.splice(index, 1)
									return;
								}
							})
							if (Object.keys(obj).length != 0) {
								this.addressList.unshift(obj);
							}
							this.showPageAddressObj = this.addressList[0]
							console.log('最后的地址', this.addressList);
						} else {
							this.showPageAddressObj = {}
						}
					}
				)
			},
			gotoAddress() {
				this.$refs.dizhiText.show();
			},
			//动态获取用户的支付方式（hlb-合利宝，zfb-支付宝，wx-微信）
			getPaymentList() {
				this.$request('order/mall/components/getPayChannelList', {}, 'get', false).then((res) => {
					if (res.data && res.data.length > 0) {
						this.zfbHasPayment = res.data.some(item => item.type == 'zfb')
						this.wxHasPayment = res.data.some(item => item.type == 'wx')
					}
				});
			},
		}
	};
</script>

<style lang="scss" scoped>
	.cut_payment_box {
		box-sizing: border-box;
		padding: 20rpx;
		position: relative;

		.dizhiText {
			box-sizing: border-box;
			padding: 30rpx 20rpx;
			position: relative;

			.newAddress {
				// position: absolute;
				// left: 50%;
				// bottom: -30rpx;
				// transform: translateX(-50%);

				width: 80%;
				background-image: linear-gradient(to right, #f9a067, #f16840, #fa3426);
				height: 70rpx;
				line-height: 70rpx;
				margin: 30rpx auto 0 auto;
				font-size: 28rpx;
				letter-spacing: 0.05em;
				color: #ffffff;
				text-align: center;
				border-radius: 50rpx;
			}

			.dizhi_tit {
				text-align: center;
				font-size: 30rpx;
				font-weight: 600;
			}

			.dizhi_item {
				display: flex;
				align-items: center;
				margin: 30rpx 0;

				.dizhi {
					font-size: 24rpx;
					margin-left: 20rpx;

					.dizhi1 {
						margin-bottom: 6rpx;
					}
				}

				.shanchuBtn {
					margin-left: auto;
				}
			}
		}

		.address-section {
			padding: 30rpx 0;
			background: #fff;
			position: relative;
			background-color: #fff4e6;
			border-bottom: 1px solid #d3d3d3;
			border-radius: 15rpx 15rpx 0 0;

			.order-content {
				display: flex;
				align-items: center;

				.iocn {
					width: 20rpx;
					height: 20rpx;
					margin-right: 20rpx;
				}

				.bottom-backgroind {
					position: absolute;
					height: 8rpx;
					width: 100%;
					bottom: 0;
					left: 0px;
				}
			}

			.icon-shouhuodizhi {
				flex-shrink: 0;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 90rpx;
				color: #888;
				font-size: 44rpx;
			}

			.cen {
				display: flex;
				flex-direction: column;
				flex: 1;
				font-size: 28rpx;
				color: #303133;
			}

			.name {
				font-size: 34rpx;
				margin-right: 24rpx;
			}

			.address {
				margin-top: 16rpx;
				margin-right: 20rpx;
				color: #909399;
			}

			.icon-you {
				font-size: 32rpx;
				color: #909399;
				margin-right: 30rpx;
			}

			.a-bg {
				position: absolute;
				left: 0;
				bottom: 0;
				display: block;
				width: 100%;
				height: 0rpx;
			}
		}

		.pro_box {
			display: flex;
			box-sizing: border-box;
			padding: 30rpx 15rpx;
			background-color: #fff4e6;
			border-radius: 0 0 15rpx 15rpx;

			.imgs {
				width: 200rpx;
				height: 200rpx;
				flex-shrink: 0;
				margin-right: 20rpx;
			}

			.pro_right {
				width: 100%;
				display: flex;
				flex-direction: column;
				justify-content: space-around;
				font-size: 24rpx;

				.protitle {
					font-size: 28rpx;
					font-weight: 600;
				}

				.old_price {
					color: #b0b0b0;
				}

				.end_time {
					color: #b0b0b0;
				}

				.cut_price {
					color: #8c6c49;
					font-weight: 600;
					font-size: 24rpx;
					padding: 5rpx 15rpx;
					border: 1px solid #8c6c49;
					width: max-content;
					border-radius: 15rpx;
				}
			}
		}

		.other_tips {
			box-sizing: border-box;
			padding: 15rpx;
			background-color: #fff4e6;
			margin: 15rpx 0;
			border-radius: 15rpx;

			.kuadi {
				display: flex;
				justify-content: space-between;
				margin: 20rpx 0;
			}
		}

		.submit_btn {
			position: fixed;
			bottom: 0;
			left: 0;
			background-color: #fff4e6;
			width: 100%;
			display: flex;
			justify-content: space-between;
			align-items: center;
			box-sizing: border-box;
			padding: 40rpx 25rpx;

			.total_price {}

			.btn {
				padding: 10rpx 40rpx;
				letter-spacing: 0.1em;
				color: #ffffff;
				border-radius: 40rpx;
				background-image: linear-gradient(to right, #f9c082, #be9263, #8c6c49);
			}
		}
	}
</style>